﻿using System;
using System.Linq.Expressions;
using System.Threading;

namespace WebSelfHost.Utility.Threads
{
    public class TaskHelper
    {
        private Thread thread;

        /// <summary>
        /// 执行线程
        /// </summary>
        /// <param name="action"></param>
        /// <param name="completeAction"></param>
        /// <param name="exceptionAction"></param>
        public TaskHelper DoTask(Action action, Action completeAction = null, Action<Exception> exceptionAction = null)
        {
            thread = new Thread(() =>
            {
                try
                {
                    action();
                }
                catch (Exception ex)
                {
                    exceptionAction?.Invoke(ex);
                }
                finally
                {
                    completeAction?.Invoke();
                }
            });
            thread.Start();
            return this;
        }

        /// <summary>
        /// 执行While(true)任务
        /// </summary>
        /// <param name="loopAction"></param>
        /// <param name="sleepTime"></param>
        /// <param name="exceptionAction"></param>
        /// <param name="interruptAction"></param>
        public TaskHelper DoWhileTrueTask(Action loopAction, int sleepTime = 1000, Action<Exception> exceptionAction = null, Action interruptAction = null)
        {
            thread = new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        loopAction();
                        Thread.Sleep(sleepTime);
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains("interrupted"))
                        {
                            interruptAction?.Invoke();
                            break;
                        }
                        exceptionAction?.Invoke(ex);
                    }

                }
            });
            thread.Start();
            return this;
        }

        public TaskHelper DoWhileTask(Func<bool> condition, Action loopAction, int sleepTime = 1000, Action<Exception> exceptionAction = null, Action interruptAction = null)
        {
            thread = new Thread(() =>
            {
                while (condition.Invoke())
                {
                    try
                    {
                        loopAction();
                        Thread.Sleep(sleepTime);
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains("interrupted"))
                        {
                            interruptAction?.Invoke();
                            break;
                        }
                        exceptionAction?.Invoke(ex);
                    }
                    
                }
            });
            thread.Start();
            return this;
        }

        /// <summary>
        /// 终止线程
        /// </summary>
        public void Stop()
        {
            try { thread.Interrupt(); } catch { }
        }
    }
}
